package com.sebastian.studies.ch9_9_7_ATM;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class ATMexecute {

	public static void main(String[] args) throws IOException {

		Scanner input = new Scanner(System.in);
		File file = new File("atm_excercise/accounts");

		if(filePresent(file)) {
			int id = login();
			Account account = obtainingAccount(file, id);

			while(accountExists(file, id)) {
				System.out.println("Main Menu\n1: check balance\n2: withdraw\n3: deposit\n4. exit");
				System.out.println("Enter your choice: ");
				int choice = input.nextInt();
					switch (choice) {
						case 1:
							System.out.println("Balance inquairy" + 
								"\nYour balance is: " + account.getBalance());
							break;
						case 2:
							System.out.println("How much whould you like to withdraw?: ");
							double withdraw = input.nextDouble();
							if(withdraw > account.getBalance()) {
								System.out.println("Sorry, insufficient founds!!!");
							} else {
								account.setBalance(account.getBalance() - withdraw);
								//TODO: method is not implemented
								//saveBalance(account, file, id);
							}
							break;
						case 3:
							System.out.println("How much would you like to deposit?: ");
							double deposit = input.nextDouble();
							account.setBalance(account.getBalance() + deposit);
							//TODO: method is not implemented
							//saveBalance(account, file, id);
							break;
						case 4:
							System.out.println("Exit!\nBye!");
							id = login();
							account = obtainingAccount(file, id);
							break;
						default: 
							System.out.println("System mulfunction");
							break;
					}
				}
		}
	}

	private static boolean filePresent(File file) {
		boolean present;
		if(file.exists()) {
			present = true;
		} else {
			present = false;
			System.out.println("An error occured. List of accounts does not exist");
		}
		return present;
	}

	private static int login() {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter your account id: ");
		int id = input.nextInt();
		return id;
	}

	private static boolean accountExists(File file, int id) throws FileNotFoundException {
		boolean accountExists = false;
		Scanner inpFromAccounts = new Scanner(file);
		Account acc = new Account(0, 0, "");
		String idString = String.valueOf(id);
		//parsing
		while(inpFromAccounts.hasNext()) {
			if (inpFromAccounts.next().contentEquals(idString)){
				String name = inpFromAccounts.next();
				//Account
				acc = new Account(id, 0, name);
			}
		}
		if(acc.getName() == "") {
			System.out.println("Sorry no such account in the system");
			return accountExists;
		}
		accountExists = true;
		return accountExists;
	}

	private static Account obtainingAccount(File file, int id) throws FileNotFoundException {
		Scanner inpFromAccounts = new Scanner(file);
		Account acc = new Account(0, 0, "");
		String idString = String.valueOf(id);
		//parsing
		while(inpFromAccounts.hasNext()) {
			if (inpFromAccounts.next().contentEquals(idString)){
				String balance = inpFromAccounts.next();
				String name = inpFromAccounts.next();
				double newBalance = Double.parseDouble(balance);
				//Account
				acc = new Account(id, newBalance, name);
			}
		}
		if(acc.getName() != "") {
			System.out.println("You are logged in as: " + acc.getName());
		}
		return acc;
	}

	private static boolean saveBalance(Account account, File file, int id) throws FileNotFoundException {
		boolean saveCompleted = false;
		if(account.getBalance() !=  obtainingAccount(file, id).getBalance()) {
			PrintWriter output = new PrintWriter(file);
			output.print(account.getBalance());
			output.close();
		} else {
			System.out.println("2");
		}
		return saveCompleted;
	}

}
